﻿#include "GraphicsMFCItem.h"
#include <QGraphicsLinearLayout>
#include <QPainter>
#include <QDebug>
#include <QInputDialog>

GraphicsMFCItem::GraphicsMFCItem()
{
    //QGraphicsLinearLayout *layout = new QGraphicsLinearLayout(Qt::Vertical, this);
}

void GraphicsMFCItem::setActualValue(double v)
{
    m_actualValue = v;
    update();
}

void GraphicsMFCItem::setDesiredValue(double v)
{
    m_desiredValue = v;
    update();
}

QRectF GraphicsMFCItem::boundingRect() const
{
    return QRectF(-75, -40, 150, 80);
}

QPointF GraphicsMFCItem::keyPoint(int i)
{
    return QPointF();
}

void GraphicsMFCItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    QPen pen(Qt::black);
    pen.setWidth(3);
    QBrush brush1(Qt::yellow), brush2(Qt::green);

    painter->setPen(pen);
    painter->setBrush(brush1);
    painter->drawRect(-75, -40, 100, 40);
    painter->setBrush(brush2);
    painter->drawRect(-75, 0, 100, 40);

    QString n1 = QString::number(m_desiredValue);
    QString n2 = QString::number(m_actualValue);

    QFont font;
    font.setPointSize(16);
    painter->setFont(font);

    painter->drawText(-70, -5, n1);
    painter->drawText(-70, 35, n2);

}

void GraphicsMFCItem::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event)
{
    qDebug() << "mouseDoubleClickEvent";
    bool isOk = false;
    double v = QInputDialog::getDouble(nullptr, u8"输入设定值", u8"输入设定值：", 0, 0, 10000, 1, &isOk, Qt::WindowFlags());
    if(isOk)
    {
        m_desiredValue = v;
        update();
        emit setPointChanged(m_desiredValue);
    }

}
